package com.ruyuan.eshop.construction.api.impl;

import com.alibaba.fastjson.JSON;
import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.common.exception.ProductBizException;
import com.ruyuan.eshop.construction.api.ProductApi;
import com.ruyuan.eshop.construction.domain.dto.ItemDetailDTO;
import com.ruyuan.eshop.construction.domain.dto.ProductDTO;
import com.ruyuan.eshop.construction.domain.dto.SaleSkuDTO;
import com.ruyuan.eshop.construction.domain.dto.SkuDetailDTO;
import com.ruyuan.eshop.construction.domain.dto.SkuPriceResultDTO;
import com.ruyuan.eshop.construction.domain.request.ProductRequest;
import com.ruyuan.eshop.construction.domain.request.QueryItemListRequest;
import com.ruyuan.eshop.construction.domain.request.QuerySaleSkuRequest;
import com.ruyuan.eshop.construction.domain.request.QuerySkuListRequest;
import com.ruyuan.eshop.construction.domain.request.SkuPriceRequest;
import com.ruyuan.eshop.construction.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 商品服务
 *
 * @author zhonghuashishan
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = ProductApi.class, retries = 0)
public class ProductApiImpl implements ProductApi {

    @Autowired
    private ProductService productService;

    /**
     * 建品/编辑商品接口
     *
     * @param request
     * @return com.ruyuan.eshop.common.core.JsonResult<com.ruyuan.eshop.construction.domain.dto.ProductDTO>
     * @author zhonghuashishan
     */
    @Override
    public JsonResult<ProductDTO> product(ProductRequest request) {
        try {
            ProductDTO productDTO = productService.product(request);
            return JsonResult.buildSuccess(productDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 修改商品价格接口
     *
     * @param request
     * @return com.ruyuan.eshop.common.core.JsonResult<com.ruyuan.eshop.construction.domain.dto.UpdatePriceDTO>
     * @author zhonghuashishan
     */
    @Override
    public JsonResult<SkuPriceResultDTO> updatePrice(SkuPriceRequest request) {
        try {
            SkuPriceResultDTO dto = productService.updatePrice(request);
            return JsonResult.buildSuccess(dto);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 查询可售sku
     *
     * @param request
     * @return com.ruyuan.eshop.common.core.JsonResult<com.ruyuan.eshop.construction.domain.dto.SaleSkuDTO>
     * @author zhonghuashishan
     */
    @Override
    public JsonResult<SaleSkuDTO> getSaleSku(QuerySaleSkuRequest request) {
        try {
            SaleSkuDTO saleSkuDTO = productService.getSaleSku(request);
            return JsonResult.buildSuccess(saleSkuDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<List<ItemDetailDTO>> listItems(QueryItemListRequest request) {
        try {
            List<ItemDetailDTO> itemDetailDTOS = productService.listItemByItemIds(request);
            return JsonResult.buildSuccess(itemDetailDTOS);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<List<SkuDetailDTO>> listSkus(QuerySkuListRequest request) {
        try {
            List<SkuDetailDTO> skuDetailDTOS = productService.listSkus(request);
            return JsonResult.buildSuccess(skuDetailDTOS);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }


}
